/*
 * @Descripttion:
 * @Author: jhw
 * @Date: 2024-04-24 17:31:20
 * @LastEditors: jhw
 * @LastEditTime: 2024-04-24 17:48:05
 */
import { vnode } from "./vnode";

/**
 *
 * @param {*} sel 选择器
 * @param {*} options 选项
 * @param {*} c  子元素
 * @example h("div", {}, "撒大家啊");
 * @example h("div", {}, [h("span", {}, "text")]);
 * @example h("div", {}, h("span", {}, "text"));
 */
export function h(sel, options, c) {
  if (arguments.length !== 3) {
    throw new Error("h函数接受三个参数");
  }
  if (typeof c === "string") {
    return vnode(sel, options, undefined, c, undefined);
  } else if (Array.isArray(c) && c.length > 0) {
    let children = [];
    for (let i = 0; i < c.length; i++) {
      if (typeof c[i] !== "object" && c[i].hasOwnProperty("sel")) {
        throw new Error("子元素不是h函数创建的vnode");
      }
      children.push(c[i]);
    }
    return vnode(sel, options, children, undefined, undefined);
  } else if (typeof c === "object" && c.hasOwnProperty("sel")) {
    return vnode(sel, options, [c], undefined, undefined);
  } else {
    throw new Error("参数不合法");
  }
}
